/*******************************************************************/
/*    Copyright (c) 1999-2020 by Spatial Corp.                     */
/*    All rights reserved.                                         */
/*    Protected by U.S. Patents 5,257,205; 5,351,196; 6,369,815;   */
/*                              5,982,378; 6,462,738; 6,941,251    */
/*    Protected by European Patents 0503642; 69220263.3            */
/*    Protected by Hong Kong Patent 1008101A                       */
/*******************************************************************/

//----------------------------- IopAcisPolicyOptions.cpp -------------------------------

//Acis
#include "lists.hxx"

//Iop
#include "SPAIop.h"
#include "SPAIopAcis.h"
#include "SPAIopArgParser.h"
#include "SPAIopPrintUtilities.h"
#include "SPAIopPolicyUtilities.h"
#include "SPAIopAcisPolicyUtilities.h"
#include "SPAIopAcisSampleUtilities.h"
#include "SPAIopPerformanceStatistics.h"


using namespace SPAIopAcisPrintUtils;
using namespace SPAIopPolicyUtilities;
using namespace SPAIopAcisPolicyUtilities;

// License file
#include "spatial_license.h"
#include "spa_unlock_result.hxx"
//-----------------------------------------------------------------------------------------
void PrintHelp()
{
	PrintMessage( L"Usage: IopAcisPolicyOptions [-p <name-of-policy>][-o <full-output-policy-file-path>] [-h] \n" );
	PrintMessage( L"\t-p  : The name of the policy to build. \n" );
	PrintMessage( L"\t      * To build a policy using the native APIs, use one of <Default|MinimalProcessing|FullProcessing|DataPrep|Custom>. Good for learning about policies. \n" );
	PrintMessage( L"\t        Default - builds an empty policy. \n");
	PrintMessage( L"\t        MinimalProcessing - shows how to build a policy with options that minimize processing. \n");
	PrintMessage( L"\t        FullProcessing - shows how to build a policy with options that enhance processing. \n");
	PrintMessage( L"\t        DataPrep - allows you to experiment with building a policy with options that allow Data Prep. \n");
	PrintMessage( L"\t        Custom - allows you to experiment with building a policy tuned to your custom workflow. \n");
	PrintMessage( L"\t      * To build a policy that controls the amount of data to process using the SPAIopPolicyFactory, use one of <MinimalData|StandardData>. Good for getting a quick start from preset options.\n");
	PrintMessage( L"\t        MinimalData - builds a policy from a factory method that suppresses some data processing. This is good for workflows that require only the BRep. \n");
	PrintMessage( L"\t        StandardData - builds a policy from a factory method that allows standars data processing. This is good for workflows that require data above and beyond the BRep. \n");
	PrintMessage( L"\t      * To build a policy that is tuned for minimal processing for benchmarking, use one of <BRepBenchmark|VisuBenchmark>. \n");
	PrintMessage( L"\t      * To build a policy that closest matches the options of Connect use <ConnectToUConnect>. \n");
	PrintMessage( L"\t-o  : The absolute path to the output policy file (.xmlExpo). \n" );
	PrintMessage( L"\t-h  : This help.\n" );
}

//-----------------------------------------------------------------------------------------

int test_IopAcisPolicyOptions(int argc, char* argv[])
{
	int result( 1 );
	SPAIopPerformanceStatistics printPerfoStatsObj;

	try
	{
		// IOP is called in its own scope.
		// This sample uses a guard class that initializes InterOp upon 
		// construction and terminates InterOp upon destruction.  All 
		// translations are done within the boundaries of a single 
		// initialization and shutdown.  InterOp may only be initialized 
		// and shutdown once per process.  Please do not use the guard 
		// classes multiple times per process.
		SPAIopAcisSystemInitGuard initGuard(SPATIAL_LICENSE);

		result = initGuard.GetInitStatus();

		if (SPA_UNLOCK_NO_MSG == result)
		{
			// Spatial products have been unlocked
			std::wcout << L"SUCCESS: SPAIopAcisSystem::Initialize. " << std::endl;
		}
		else if (SPA_UNLOCK_KEY_WARN_EXPIRE == result)
			std::wcout << L"SPAIopAcisSystem::Initialize returned SPA_UNLOCK_KEY_WARN_EXPIRE; your license key will expire soon!\n";
		else if (SPA_UNLOCK_REGISTRATION_WARN_EXPIRE == result)
			std::wcout << L"SPAIopAcisSystem::Initialize returned SPA_UNLOCK_REGISTRATION_WARN_EXPIRE; your registration will expire soon.\n";
		else
		{
			std::wcout << L"SPAIopAcisSystem::Initialize returned an unexpected value; Contact Spatial Customer Support or your Account Manager.\n";
			return 1;
		}
		result = 1;


	char* argFields[] = { "p", "o", "h" };
	int size = sizeof( argFields ) / sizeof( *argFields );
    SPAIopArgParser parser( size, argFields, argc, argv, &PrintHelp );

	if ( true == parser.IsSuccess())
	{
		const wchar_t* pPolicyName = 0;
		bool hasPolicyName = parser.GetWStringValueFor( "p",  pPolicyName );

		const wchar_t* pOutputPolicyFilePath = 0;
		bool hasOutputFileName = parser.GetWStringValueFor( "o",  pOutputPolicyFilePath );

		bool success( false );
		try
		{
			SPAIopPolicy policy;
			if ( true == hasPolicyName )
			{
				SPAIopPolicyUtilities::AddModelerOptionsCBFct = AddAcisModelerOptionsCB;

				std::wstring policyNameStr( pPolicyName );
				SPAIopWString policyName( policyNameStr.c_str());
				policy = BuildPolicy( policyName );
				success = true;
			}

			if ( true == hasOutputFileName )
			{
				std::wstring outputPolicyPathStr( pOutputPolicyFilePath );
				SPAIopWString outputPolicyFilePath( outputPolicyPathStr.c_str());
				success = GeneratePolicyFile( policy, outputPolicyFilePath );
			}
		}
		catch ( SPAIopAcisError& error )
		{
			DisplayError( error );
			PrintMessage( L"Caught SPAIopAcisError while building policy. \n" );
			success = false;
		}
		catch ( ... )
		{
			std::wcout << "FAILURE: Unknown exception caught! " << std::endl;
			success = false;
		}

		if ( true == success )
			result = 0;
	}

	}
	catch (SPAIopAcisError& fatalErr)
	{
		DisplayError(fatalErr);
		result = 1;
	}
	catch (...)
	{
		PrintMessage(L"FAILURE: Unknown exception caught! ");
		result = 1;
	}

    return result;
} 

//-----------------------------------------------------------------------------------------

